package com.xiaoxin.smartbi.mqtest.mq5;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogsTopic {

  private static final String EXCHANGE_NAME = "topic_logs";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();

    String queueName1 = "a";
    Channel channel1 = connection.createChannel();
    channel1.exchangeDeclare(EXCHANGE_NAME, "topic");
    channel1.queueDeclare(queueName1, true, false, false, null);
    channel1.queueBind(queueName1, EXCHANGE_NAME, "#.a.#");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(queueName1+"  Received '" +
            delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel1.basicConsume(queueName1, true, deliverCallback1, consumerTag -> { });

    
      String queueName2 = "b";
      Channel channel2 = connection.createChannel();
      channel2.exchangeDeclare(EXCHANGE_NAME, "topic");
      channel2.queueDeclare(queueName2, true, false, false, null);
      channel2.queueBind(queueName2, EXCHANGE_NAME, "#.b.#");
      System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

      DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
          String message = new String(delivery.getBody(), "UTF-8");
          System.out.println(queueName2+"  Received '" +
                  delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
      };
      channel2.basicConsume(queueName2, true, deliverCallback2, consumerTag -> { });

      String queueName3 = "c";
      Channel channel3 = connection.createChannel();
      channel3.exchangeDeclare(EXCHANGE_NAME, "topic");
      channel3.queueDeclare(queueName3, true, false, false, null);
      channel3.queueBind(queueName3, EXCHANGE_NAME, "#.c.#");
      System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

      DeliverCallback deliverCallback3 = (consumerTag, delivery) -> {
          String message = new String(delivery.getBody(), "UTF-8");
          System.out.println(queueName3+"  Received '" +
                  delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
      };
      channel3.basicConsume(queueName3, true, deliverCallback3, consumerTag -> { });
  }
}